package com.zhou.jiancai.Internet;


import com.zhou.jiancai.Utils.NetWorkUtils;
import com.zhou.jiancai.Utils.ToastUtils;
import com.zhou.jiancai.common.BasicParamsInterceptor;
import com.zhou.jiancai.common.Constants;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by FangJie on 15/12/28.
 */

/*
 * Copyright (C) 2012 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


public class ServiceFactory {


    private static OkHttpClient client;

    public static <T> T createRetrofitService(final Class<T> clazz) {
        //设置打印log的样式
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .build();

        if (!NetWorkUtils.isNetConnect()) {
            ToastUtils.showShort("请检查你的网络连接");
            return null;
        }

        Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.Newworkinfo)
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())

                .addConverterFactory(GsonConverterFactory.create())
                .build();
        T service = retrofit.create(clazz);

        return service;
    }

    public static <T> T createNewRetrofitService(final Class<T> clazz) {


        BasicParamsInterceptor basicParamsInterceptor =
                new BasicParamsInterceptor.Builder()
                        .addParam("app_name", "mali")
                        .addParam("app_version", "10005")
                        .addParam("type", "android")
                        .build();
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(basicParamsInterceptor)
                .addInterceptor(interceptor)

                .build();

        if (!NetWorkUtils.isNetConnect()) {
            ToastUtils.showShort("请检查你的网络连接");
            return null;
        }
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.NEW_API_HOST)
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())

                .addConverterFactory(GsonConverterFactory.create())
                .build();
        T service = retrofit.create(clazz);

        return service;
    }

    public static <T> T createRetrofitService(final Class<T> clazz, String host) {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client = new OkHttpClient.Builder()
                .addInterceptor(interceptor).connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .build();

        if (!NetWorkUtils.isNetConnect()) {
            ToastUtils.showShort("请检查你的网络连接");
            return null;
        }
        Retrofit retrofit = new Retrofit.Builder().baseUrl(host)
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())

                .addConverterFactory(GsonConverterFactory.create())
                .build();
        T service = retrofit.create(clazz);

        return service;
    }
}